at android.os.MessageQueue.nativePollOnce(Native method) at android.os.MessageQueue.next(MessageQueue.java:323) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke!(Native method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() { @Override publicvoidonClick(View v){ final Thread mainThread = Thread.currentThread(); Log.i("<<<", mainThread.getState().toString()); if (thread == null) { thread = new Thread(new Runnable() { @Override publicvoidrun(){ for (int i = 0; i < 3; ++i) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } Log.i("<<<", mainThread.getState().toString()); } } }); thread.start(); } thrownew RuntimeException("hello world!!"); } }); } }
上面代码的第 24 行和第 35 行一共会 4 此输出主线程的状态,结果如下面所示:
1 2 3 4
01-18 21:00:24.308 14533 14533 I <<< : RUNNABLE 01-18 21:00:25.309 14533 14570 I <<< : NEW 01-18 21:00:26.310 14533 14570 I <<< : NEW 01-18 21:00:27.311 14533 14570 I <<< : NEW
从上面代码可知,主线程从 RUNNABLE 状态变成了 NEW 状态,为什么是 NEW 状态,我也不清楚,也许将来对 JVM 了解更多了,会清除吧。